//
//  MessageView.swift
//  LeGou
//
//  Created by lxm on 2022/10/8.
//

import UIKit

class MessageView: ZMXIBView {

    //MARK: --------------------属性--------------------
    var labelY : CGFloat {
        (contentV.zm.height - labelHeight) / 2
    }
    
    var labelHeight : CGFloat {
        return UIFont.systemFont(ofSize: 12).lineHeight
    }
    
    var labelWidth : CGFloat {
        contentV.zm.size.width - 62 - 12
    }
    
    var labelX : CGFloat {
        return 50 + 12
    }
    
    var labelFrame : CGRect {
        return CGRect(x: labelX, y: labelY, width: labelWidth , height: labelHeight)
    }
    
    var outLabelFrame : CGRect {
        return CGRect(x: 50 + 12, y: contentV.zm.height, width: labelWidth , height: labelHeight)
    }
    
    let timer = Observable<Int>.timer(.seconds(0), period: .seconds(3), scheduler: MainScheduler.instance)
    
    var currentIndex = 0
    
    var datas = [String]()
    
    var timerDisposeBag = DisposeBag()
//    var timer : Timer!
    
    //MARK: --------------------UI控件--------------------
    @IBOutlet weak var contentV: UIView!
    
    let lableOne = ZMUtil.label(title: "", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12))
    let lableTwo = ZMUtil.label(title: "", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12))
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        contentV.addSubview(lableOne)
        getInfo()
    }
    
    func addConstraints() {
     
    }
    
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    @objc func fuckOff() {
        
    }
    
    @objc func goNext() {
        guard datas.count > 0 else {return}
        currentIndex = currentIndex + 1 < datas.count ? currentIndex + 1 : 0
        let nextTitle = datas[currentIndex]
        contentV.addSubview(lableTwo)
        lableTwo.frame = outLabelFrame
        lableTwo.text = nextTitle
        UIView.animate(withDuration: 0.5) {
            self.lableOne.frame = CGRect(x: 50 + 12, y: -self.labelHeight, width: self.labelWidth , height: self.labelHeight)
            self.lableTwo.frame = self.labelFrame
        } completion: { flag in
            self.lableOne.frame = self.labelFrame
            self.lableTwo.removeFromSuperview()
            self.lableOne.text = self.lableTwo.text
        }
    }
    
    func getInfo() {
        if AppHelper.appVerify() {return}
        RedBagAPI.red_rank(["range" : 3, "type" : 1]).rxRequest(type: [RedBagModels.Rank].self).subscribe(onNext:{ [unowned self] resp in
            let r = resp.map { m in
                return m.nickname + " 抢红包获得" + " ¥\(m.amount.zm.toString(keep: 2, true))"
            }
            self.datas = r
            self.reload()
        }, onError: { _ in
        }, onCompleted: {
        }).disposed(by: disposeBag)
    }
 
    func reload() {
        timerDisposeBag = DisposeBag()
        self.currentIndex = 0
        lableOne.text = datas.first ?? ""
        lableOne.frame = labelFrame
        contentV.layer.masksToBounds = true
        contentV.layer.cornerRadius = 8
        timer.subscribe(onNext:{ [unowned self] resp in
            self.goNext()
        }).disposed(by: timerDisposeBag)
    }
    
    
    //MARK: --------------------delegate--------------------

}
